﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Eff.Utils
{
    ///<summary>
    ///</summary>
    public static class AttributeHelper
    {
        /// <summary>
        /// 得到实体的属性值
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetDescriptionDictionary<T>(EntityTpe entityTpe)
        {
            var dicTem = new Dictionary<string, string>(20);
            Type type = typeof(DescriptionAttribute);
            switch (entityTpe)
            {
                case EntityTpe.Enum:
                    foreach (var fi in typeof(T).GetFields())
                    {
                        object[] arr = fi.GetCustomAttributes(type, true);
                        if (arr.Length > 0)
                        {
                            dicTem.Add(fi.Name, ((DescriptionAttribute)arr[0]).Description);
                        }
                    }
                    break;
                case EntityTpe.Class:
                    foreach (var fi in typeof(T).GetProperties())
                    {
                        object[] arr = fi.GetCustomAttributes(type, true);
                        if (arr.Length > 0)
                        {
                            dicTem.Add(fi.Name, ((DescriptionAttribute)arr[0]).Description);
                        }
                    }
                    break;
            }

            return dicTem;
        }
      }
    /// <summary>
    /// 实体类型
    /// </summary>
    public enum EntityTpe
    {
        /// <summary>
        /// 枚举
        /// </summary>
        Enum,
        /// <summary>
        /// 类
        /// </summary>
        Class
    }
}
